package com.luobr.backend.config;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: 罗念笙
 * @date: 2025/2/23 17:09
 */
@Configuration
@ConfigurationProperties(prefix = "oss")
@Data
public class OssClientConfig {

    /**
     * 域名
     */
    private String endpoint;

    /**
     * secretId
     */
    private String accessKeyId;

    /**
     * 密钥（注意不要泄露）
     */
    private String accessKeySecret;

    /**
     * 桶名
     */
    private String bucketName;

    /**
     * 最大连接数
     */
    private String maxConnections;

    /**
     * 连接超时
     */
    private String connectionTimeout;
    
    /**
     * 会话超时
     */
    private String socketTimeout;


    @Bean
    public OSS ossClient() {
        // 客户端配置（优化性能）
        ClientBuilderConfiguration config = new ClientBuilderConfiguration();
        config.setMaxConnections(Integer.parseInt(maxConnections));
        config.setConnectionTimeout(Integer.parseInt(connectionTimeout));
        config.setSocketTimeout(Integer.parseInt(socketTimeout));
        config.setCrcCheckEnabled(true);
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, config);
    }
}